home *** CD-ROM | disk | FTP | other *** search
/ Delphi 5 for Professionals / DELPHI5.iso / AddOns / Components / TEECHART / Src Code / CANDLECH.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-10-24  |  12.2 KB  |  409 lines

  1. {**********************************************}
  2. {   TCandleSeries (derived from OHLCSeries)    }
  3. {   Copyright (c) 1995-1996 by David Berneda   }
  4. {**********************************************}
  5. {$I teedefs.inc}
  6. unit CandleCh;
  7.  
  8. interface
  9.  
  10. { This unit shows how a new Chart Series component can be easily created.
  11.   TCandleSeries derives from TOHLCSeries (Open, High, Low & Close).
  12.   See OHLChart.pas for TOHLCSeries source code.
  13.  
  14.   TCandleSeries overrides the TChartSeries.DrawValue method to paint its
  15.   points in a financial used fashion.
  16.  
  17.   TVolumeSeries overrides the TChartSeries.DrawValue method to paint its
  18.   points like thin vertical bars.
  19. }
  20. Uses WinTypes,WinProcs,Classes,Graphics,Chart,Series,OHLChart,Teengine,
  21.      TeCanvas;
  22.  
  23. Const DefCandleWidth = 5;  { 2 + 1 + 2 }
  24.  
  25. Type TCandleStyle=(csCandleStick,csCandleBar);
  26.  
  27.      { TCandleSeries }
  28.      TCandleSeries=class(TOHLCSeries)
  29.      private
  30.        FCandleWidth    : Integer;
  31.        FCandleStyle    : TCandleStyle;
  32.        FUpCloseColor   : TColor;
  33.        FDownCloseColor : TColor;
  34.        FShowOpenTick   : Boolean;
  35.        FShowCloseTick  : Boolean;
  36.      protected
  37.        procedure DrawValue(ValueIndex:Longint); override;
  38.        Procedure SetShowOpenTick(Value:Boolean);
  39.        Procedure SetShowCloseTick(Value:Boolean);
  40.        Procedure SetUpColor(Value:TColor);
  41.        Procedure SetDownColor(Value:TColor);
  42.        Procedure SetCandleWidth(Value:Integer);
  43.        Procedure SetCandleStyle(Value:TCandleStyle);
  44.        Function GetDraw3D:Boolean;
  45.        procedure SetDraw3D(Value:Boolean);
  46.        Function GetDark3D:Boolean;
  47.        procedure SetDark3D(Value:Boolean);
  48.        Function GetPen:TPen;
  49.        procedure SetPen(Value:TPen);
  50.      public
  51.        Constructor Create(AOwner: TComponent); override;
  52.        Procedure PrepareForGallery(IsEnabled:Boolean); override;
  53.        Procedure Assign(Source:TPersistent); override;
  54.        Function AddCandle( Const ADate:TDateTime;
  55.                            Const AOpen,AHigh,ALow,AClose:Double):Longint;
  56.        Function  GetEditorClass:String; override;
  57.     published
  58.        property CandleStyle:TCandleStyle read FCandleStyle write SetCandleStyle
  59.                                          default csCandleStick;
  60.        property CandleWidth:Integer read FCandleWidth write SetCandleWidth
  61.                                     default DefCandleWidth;
  62.        property Draw3D:Boolean read GetDraw3D write SetDraw3D default False;
  63.        property Dark3D:Boolean read GetDark3D write SetDark3D default True;
  64.        property DownCloseColor:TColor read FDownCloseColor write SetDownColor
  65.                                       default clRed;
  66.        property ShowCloseTick:Boolean read FShowCloseTick write SetShowCloseTick
  67.                                       default True;
  68.        property ShowOpenTick:Boolean read FShowOpenTick write SetShowOpenTick
  69.                                      default True;
  70.        property UpCloseColor:TColor read FUpCloseColor write SetUpColor
  71.                                     default clWhite;
  72.        property Pen:TPen read GetPen write SetPen;
  73.      end;
  74.  
  75.      { Used in financial charts for Volume quantities (or OpenInterest) }
  76.      { Overrides FillSampleValues to create random POSITIVE values }
  77.      { Overrides DrawValue to paint a thin vertical bar }
  78.      { Declares VolumeValues (same like YValues) }
  79.      TVolumeSeries=class(TCustomSeries)
  80.      protected
  81.        Function GetVolumeValues:TChartValueList;
  82.        Procedure SetVolumeValues(Value:TChartValueList);
  83.        procedure DrawValue(ValueIndex:Longint); override;
  84.      public
  85.        Constructor Create(AOwner: TComponent); override;
  86.        Function GetEditorClass:String; override;
  87.        Function NumSampleValues:Longint; override;
  88.        Procedure FillSampleValues(NumValues:Longint); override;
  89.        Procedure PrepareForGallery(IsEnabled:Boolean); override;
  90.      published
  91.        property LinePen;
  92.        property VolumeValues:TChartValueList read GetVolumeValues write SetVolumeValues;
  93.        property XValues;
  94.      end;
  95.  
  96. implementation
  97.  
  98. Uses SysUtils,TeeProCo,TeeConst;
  99.  
  100. { TCandleSeries }
  101. Constructor TCandleSeries.Create(AOwner: TComponent);
  102. Begin
  103.   inherited Create(AOwner);
  104.   FUpCloseColor  :=clWhite;
  105.   FDownCloseColor:=clRed;
  106.   FCandleWidth   :=DefCandleWidth;
  107.   FCandleStyle   :=csCandleStick;
  108.   FShowOpenTick  :=True;
  109.   FShowCloseTick :=True;
  110.   Pointer.Draw3D :=False;
  111.   {$IFDEF TEETRIAL}
  112.   TeeTrial(ComponentState);
  113.   {$ENDIF}
  114. end;
  115.  
  116. Procedure TCandleSeries.SetShowOpenTick(Value:Boolean);
  117. Begin
  118.   SetBooleanProperty(FShowOpenTick,Value);
  119. End;
  120.  
  121. Procedure TCandleSeries.SetShowCloseTick(Value:Boolean);
  122. Begin
  123.   SetBooleanProperty(FShowCloseTick,Value);
  124. End;
  125.  
  126. procedure TCandleSeries.DrawValue(ValueIndex:Longint);
  127. var yOpen  : Longint;
  128.     yClose : Longint;
  129.     yHigh  : Longint;
  130.     yLow   : Longint;
  131.  
  132.     Function CalcCandleColor:TColor;
  133.     Begin
  134.       With ParentChart.Canvas do
  135.       begin
  136.         if yOpen>yClose then result:=FUpCloseColor   else
  137.         if yOpen<yClose then result:=FDownCloseColor else
  138.         Begin
  139.           { color algorithm when open is equal to close }
  140.           if ValueIndex=0 then
  141.              result:=FUpCloseColor  { <-- first point }
  142.           else
  143.           if CloseValues.Value[ValueIndex-1]>CloseValues.Value[ValueIndex] then
  144.              result:=FDownCloseColor
  145.           else
  146.           if CloseValues.Value[ValueIndex-1]<CloseValues.Value[ValueIndex] then
  147.              result:=FUpCloseColor
  148.           else
  149.              result:=ValueColor[ValueIndex-1];
  150.         end;
  151.       end;
  152.     end;
  153.  
  154. Var x             : Integer;
  155.     tmpLeftWidth  : Integer;
  156.     tmpRightWidth : Integer;
  157.     tmpTop        : LongInt;
  158.     tmpBottom     : LongInt;
  159. Begin
  160.   Pointer.PrepareCanvas(clTeeColor); { Pointer Pen and Brush styles }
  161.   With ParentChart,Canvas do
  162.   Begin
  163.     X:=CalcXPosValue(DateValues.Value[ValueIndex]); { The horizontal position }
  164.  
  165.     { Vertical positions of Open, High, Low & Close values for this point }
  166.     YOpen :=CalcYPosValue(OpenValues.Value[ValueIndex]);
  167.     YHigh :=CalcYPosValue(HighValues.Value[ValueIndex]);
  168.     YLow  :=CalcYPosValue(LowValues.Value[ValueIndex]);
  169.     YClose:=CalcYPosValue(CloseValues.Value[ValueIndex]);
  170.  
  171.     tmpLeftWidth:=FCandleWidth div 2; { calc half Candle Width }
  172.     tmpRightWidth:=FCandleWidth-tmpLeftWidth;
  173.  
  174.     if FCandleStyle=csCandleStick then
  175.     Begin { draw Candle Stick }
  176.  
  177.       if View3D and Pointer.Draw3D then
  178.       begin
  179.         tmpTop:=yClose;
  180.         tmpBottom:=yOpen;
  181.         if tmpTop>tmpBottom then SwapLongint(tmpTop,tmpBottom);
  182.         { Draw Candle Vertical Line from bottom to Low }
  183.         VertLine3D(x,tmpBottom,yLow,MiddleZ);
  184.         { Draw 3D Candle }
  185.         Brush.Color:=CalcCandleColor;
  186.         if yOpen=yClose then Pen.Color:=CalcCandleColor;
  187.         Cube( x-tmpLeftWidth,x+tmpRightWidth,tmpTop,tmpBottom,
  188.               StartZ,EndZ,Pointer.Dark3D);
  189.         { Draw Candle Vertical Line from Top to High }
  190.         VertLine3D(x,tmpTop,yHigh,MiddleZ);
  191.       end
  192.       else
  193.       begin
  194.         { Draw Candle Vertical Line from High to Low }
  195.         VertLine3D(x,yLow,yHigh,MiddleZ);
  196.         { remember that Y coordinates are inverted }
  197.         if yOpen=yClose then
  198.         begin
  199.           Pen.Color:=CalcCandleColor;
  200.           HorizLine3D(x-tmpLeftWidth,x+tmpRightWidth,yOpen,MiddleZ);
  201.         end
  202.         else
  203.         begin
  204.           Brush.Color:=CalcCandleColor;
  205.           RectangleWithZ(Rect(x-tmpLeftWidth,yOpen,x+tmpRightWidth,yClose),MiddleZ);
  206.         end;
  207.       end;
  208.     end
  209.     else
  210.     Begin { draw Candle bar }
  211.       Pen.Color:=CalcCandleColor;
  212.       { Draw Candle Vertical Line from High to Low }
  213.       VertLine3D(x,yLow,yHigh,MiddleZ);
  214.       if FShowOpenTick then HorizLine3D(x,x-tmpLeftWidth,yOpen,MiddleZ);
  215.       if FShowCloseTick then HorizLine3D(x,x+tmpRightWidth,yClose,MiddleZ);
  216.     end;
  217.   end;
  218. end;
  219.  
  220. Procedure TCandleSeries.SetUpColor(Value:TColor);
  221. Begin
  222.   SetColorProperty(FUpCloseColor,Value);
  223. end;
  224.  
  225. Procedure TCandleSeries.SetDownColor(Value:TColor);
  226. Begin
  227.   SetColorProperty(FDownCloseColor,Value);
  228. end;
  229.  
  230. Procedure TCandleSeries.SetCandleWidth(Value:Integer);
  231. Begin
  232.   SetIntegerProperty(FCandleWidth,Value);
  233. end;
  234.  
  235. Procedure TCandleSeries.SetCandleStyle(Value:TCandleStyle);
  236. Begin
  237.   if FCandleStyle<>Value then
  238.   begin
  239.     FCandleStyle:=Value;
  240.     Repaint;
  241.   end;
  242. end;
  243.  
  244. Function TCandleSeries.GetEditorClass:String;
  245. Begin
  246.   result:='TCandleEditor';  { <-- do not translate }
  247. End;
  248.  
  249. Procedure TCandleSeries.PrepareForGallery(IsEnabled:Boolean);
  250. Begin
  251.   inherited PrepareForGallery(IsEnabled);
  252.   FillSampleValues(4);
  253.   ColorEachPoint:=True;
  254.   if IsEnabled then
  255.      UpCloseColor:=clBlue
  256.   else
  257.   begin
  258.     UpCloseColor:=clSilver;
  259.     DownCloseColor:=clSilver;
  260.     Pointer.Pen.Color:=clGray;
  261.   end;
  262.   Pointer.Pen.Width:=2;
  263.   CandleWidth:=12;
  264. end;
  265.  
  266. Procedure TCandleSeries.Assign(Source:TPersistent);
  267. begin
  268.   if Source is TCandleSeries then
  269.   With TCandleSeries(Source) do
  270.   begin
  271.     Self.FCandleWidth   :=FCandleWidth;
  272.     Self.FCandleStyle   :=FCandleStyle;
  273.     Self.FUpCloseColor  :=FUpCloseColor;
  274.     Self.FDownCloseColor:=FDownCloseColor;
  275.     Self.FShowOpenTick  :=FShowOpenTick;
  276.     Self.FShowCloseTick :=FShowCloseTick;
  277.   end;
  278.   inherited Assign(Source);
  279. end;
  280.  
  281. Function TCandleSeries.GetDraw3D:Boolean;
  282. begin
  283.   result:=Pointer.Draw3D;
  284. end;
  285.  
  286. procedure TCandleSeries.SetDraw3D(Value:Boolean);
  287. begin
  288.   Pointer.Draw3D:=Value;
  289. end;
  290.  
  291. Function TCandleSeries.GetDark3D:Boolean;
  292. begin
  293.   result:=Pointer.Dark3D;
  294. end;
  295.  
  296. procedure TCandleSeries.SetDark3D(Value:Boolean);
  297. begin
  298.   Pointer.Dark3D:=Value;
  299. end;
  300.  
  301. Function TCandleSeries.GetPen:TPen;
  302. begin
  303.   result:=Pointer.Pen;
  304. end;
  305.  
  306. procedure TCandleSeries.SetPen(Value:TPen);
  307. begin
  308.   Pointer.Pen.Assign(Value);
  309. end;
  310.  
  311. Function TCandleSeries.AddCandle( Const ADate:TDateTime;
  312.                                   Const AOpen,AHigh,ALow,AClose:Double):Longint;
  313. begin
  314.    result:=AddOHLC( ADate, AOpen, AHigh, ALow, AClose );
  315. end;
  316.  
  317. { TVolumeSeries }
  318. Constructor TVolumeSeries.Create(AOwner: TComponent);
  319. begin
  320.   inherited Create(AOwner);
  321.   DrawArea:=False;
  322.   Pointer.Visible:=False;
  323.   {$IFDEF TEETRIAL}
  324.   TeeTrial(ComponentState);
  325.   {$ENDIF}
  326. end;
  327.  
  328. Function TVolumeSeries.GetVolumeValues:TChartValueList;
  329. Begin
  330.   result:=YValues;
  331. end;
  332.  
  333. Procedure TVolumeSeries.SetVolumeValues(Value:TChartValueList);
  334. Begin
  335.   SetYValues(Value);
  336. end;
  337.  
  338. procedure TVolumeSeries.DrawValue(ValueIndex:Longint);
  339. var tmpY : Integer;
  340. Begin
  341.   With ParentChart,Canvas do
  342.   Begin
  343.     Pen.Assign(LinePen);
  344.     Pen.Color:=ValueColor[ValueIndex];  { <-- assign point color }
  345.     { moves to x,y coordinates and draws a vertical bar to top or bottom,
  346.       depending on the vertical Axis.Inverted property }
  347.     BackMode:=cbmTransparent;
  348.     With GetVertAxis do
  349.     if Inverted then tmpY:=IStartPos else tmpY:=IEndPos;
  350.     VertLine3D(CalcXPos(ValueIndex),CalcYPos(ValueIndex),tmpY,MiddleZ);
  351.   end;
  352. end;
  353.  
  354. Function TVolumeSeries.NumSampleValues:Longint;
  355. Begin
  356.   result:=40; { same TOHLCSeries }
  357. end;
  358.  
  359. Procedure TVolumeSeries.FillSampleValues(NumValues:Longint);
  360. Var tmpX,
  361.     StepX,
  362.     tmpY,
  363.     MinY,
  364.     DifY  : Double; { temporal variables }
  365.     t     : Longint;
  366. Begin
  367.   Clear; { delete all points }
  368.   CalcRandomBounds(NumValues,tmpX,StepX,tmpY,MinY,DifY);  { get random limits }
  369.   tmpY:=Random(Round(DifY) div 15);
  370.   for t:=1 to NumValues do
  371.   Begin
  372.     tmpY:=Random(Round(DifY) div 15);
  373.     AddXY(tmpX,tmpY{$IFNDEF D4},'', clTeeColor{$ENDIF});
  374.     tmpX:=tmpX+StepX;
  375.   end;
  376.   RefreshSeries;
  377. End;
  378.  
  379. Procedure TVolumeSeries.PrepareForGallery(IsEnabled:Boolean);
  380. begin
  381.   inherited PrepareForGallery(IsEnabled);
  382.   FillSampleValues(26);
  383.   Pointer.InflateMargins:=True;
  384. end;
  385.  
  386. Function TVolumeSeries.GetEditorClass:String;
  387. Begin
  388.   result:='TVolumeSeriesEditor';
  389. End;
  390.  
  391. Procedure TeeCandleExitProc; far;
  392. begin
  393.   UnRegisterTeeSeries([TCandleSeries,TVolumeSeries]);
  394. end;
  395.  
  396. initialization
  397.   RegisterTeeSeries( TCandleSeries, TeeMsg_GalleryCandle,
  398.                      TeeMsg_GalleryExtended, 1);
  399.   RegisterTeeSeries( TVolumeSeries, TeeMsg_GalleryVolume,
  400.                      TeeMsg_GalleryExtended, 1);
  401. {$IFDEF D1}
  402.   AddExitProc(TeeCandleExitProc);
  403. {$ENDIF}
  404. {$IFNDEF D1}
  405. finalization
  406.   TeeCandleExitProc;
  407. {$ENDIF}
  408. end.
  409.